package cn.tedu.csmall.product.controller;


import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.security.LoginPrincipal;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "01.类别管理模块")
@Slf4j
@Validated

public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/category/add-new')")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(CategoryAddNewParam categoryAddNewParam){

        log.debug("开始处理【添加属性模板】的请求,参数：{}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        return JsonResult.success();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/pms/category/delete')")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CategoryId" , value = "类别ID", required = true , dataType = "long"),
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除类别失败，请提交合法的ID值！")
                             @RequestParam Long  id){
        log.debug("开始处理[根据ID删除类别]的请求，参数:{}",id);
        categoryService.delete(id);
        return JsonResult.deleteSuccess();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@RequestParam @Range(min = 1, message = "请提交有效的类别ID值！") Long id,
                             @Valid CategoryUpdateInfoParam categoryUpdateInfoParam){
        log.debug("开始处理[修改类别详情]的业务,参数:{}",categoryUpdateInfoParam);

        categoryService.updateById(id,categoryUpdateInfoParam);
        return JsonResult.updatesuccess();
    }

    @GetMapping("/list")
    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",paramType = "query")
    }
    )
    public JsonResult list(@Range(min = 1,message = "查询类别列表失败，请提供正确的页码值！") Integer page,
                           @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("当事人ID：{}", loginPrincipal.getId());
        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        log.debug("开始处理[查询类别列表]的请求，页码：{}",page);
        if (page == null || page<1){
            page = 1;
        }
        PageData<CategoryListItemVO> pageData = categoryService.list(page);
        return JsonResult.success(pageData);
    }



    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1,message = "根据ID查询类别详情失败，请提交合法的ID值！")
                                      @RequestParam Long id){
        log.debug("开始处理[根据ID查询类别详情]的请求，参数:{}",id);
        CategoryStandardVO result = categoryService.getStandardById(id);

        return JsonResult.success(result);
    }

    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("获取类别树")
    @ApiOperationSupport(order = 500)
    public JsonResult listTree (){
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<CategoryTreeItemVO> categoryTree = categoryService.listTree();
        return JsonResult.success(categoryTree);
    }

    @PostMapping("/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id){

        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.success();

    }

    @PostMapping("/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 320)
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id){

        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.success();

    }


}
